#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <iomanip>
#include <locale>
#include <sstream>
#include <map>
using namespace std;

#define MAX_SYMBOLS 27
int main(int argc, char **argv)
{
#if 1 
    string symbol[] = {"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
#else
    string symbol[] = {"", "A", "B"};
#endif


    ofstream stock_file ;
    stock_file.open("stock_names.txt");
    map<string, int> stock_hash;
    for(string *p  = &symbol[0] ; p != &symbol[MAX_SYMBOLS]; ++p)
        for(string *q  = &symbol[0] ; q != &symbol[MAX_SYMBOLS]; ++q)
            for(string *r  = &symbol[0] ; r != &symbol[MAX_SYMBOLS]; ++r)
                for(string *s  = &symbol[0] ; s != &symbol[MAX_SYMBOLS]; ++s)
                {
                    string a = *p + *q + *r + *s;

                    if (stock_hash[a])
                        continue;
                    else
                        stock_hash[a] = 1;

                    string c = "curl -C \"tmp.csv\" \"http://download.finance.yahoo.com/d/quotes.csv?s="+ a + "&f=sj1&gl=d&c=2008&ignore=.csv\" | tee output/" + a + ".csv";
                    const char *d = c.c_str();
                    system(d);
                    //Now make sense out of the data 
                    ifstream myfile;

                    a = "./output/"+a+".csv";
                    const char *c_file = a.c_str();
                    myfile.open(c_file);

                    // Runthis while there are elements in the file
                    while (myfile.good()) {
                        string line;
                        getline(myfile, line);

                        // Rotating position for the occurence of comma
                        size_t found, loop_found;
                        double result;

                        found = line.find_first_of(",");
                        string num, name = line.substr(0, found);

                        // Convert the string to a number

                        istringstream convert(name);
                        if (!(convert >> result)) {
                            result = 0;
                        }
                        else 
                            cout << result << endl;
                        while(found!=string::npos) {
                            loop_found = line.find_first_of(",", found + 1);
                            num = line.substr(found + 1, loop_found);
                            size_t bfind = num.find("B");
                            if (bfind != string::npos) {
                                //output the name in the file
                                stock_file << name.substr(1, name.size() - 2) << endl;    
                            }

                            found = loop_found;
                        }
                    }
                    // Close my file
                    myfile.close(); 
                }
    stock_file.close();
}
